/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Time;
import java.util.Date;

/**
 *
 * @author frances
 */
public class AdminManager {

    public boolean addUserManager(User newUser) {
        boolean result = false;

        try {
            //get connection
            Model.DBConnection db = new DBConnection();
            Connection conn = db.getConnection();

            PreparedStatement pstmt = conn.prepareStatement("INSERT "
                    + "INTO user(AccountID,FirstName,LastName,MiddleInitial,Username,Password,Email,UserType,Status)"
                    + " values(?,?,?,?,?,DES_ENCRYPT(?),?,?,?)");

            //append values for the parameters
            int i = 1;
            pstmt.setString(i++, newUser.getAccountID());
            pstmt.setString(i++, newUser.getFirstName());
            pstmt.setString(i++, newUser.getLastName());
            pstmt.setString(i++, newUser.getMiddleInitial());
            pstmt.setString(i++, newUser.getUsername());
            pstmt.setString(i++, newUser.getPassword());
            pstmt.setString(i++, newUser.getEmail());
            pstmt.setInt(i++, newUser.getUserType());
//            if (newUser.getUserType() == 1) {
//                pstmt.setString(i++, "1");
//            }
//
//            if (newUser.getUserType() == 2) {
//                pstmt.setString(i++, "2");
//            }
//
//            if (newUser.getUserType() == 3) {
//                pstmt.setString(i++, "3");
//            }
//
//            if (newUser.getUserType() == 4) {
//                pstmt.setString(i++, "4");
//            }
//
//            if (newUser.getUserType() == 7) {
//                pstmt.setString(i++, "7");
//            }

            pstmt.setInt(i++, 2);

            //execute SQL statement
            pstmt.executeUpdate();

            result = true;

        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return result;
    }

    public boolean addManager(DateTime datetime) {
        boolean result2 = false;

        try {
            //get connection
            Model.DBConnection db = new DBConnection();
            Connection conn = db.getConnection();

            PreparedStatement pstmt = conn.prepareStatement("INSERT "
                    + "INTO addmanager(AccountID,Time,Date)"
                    + " values(?,?,?)");

            //append values for the parameters
            int i = 1;

            pstmt.setString(i++, datetime.getAccountID());
            pstmt.setString(i++, datetime.getTime());
            pstmt.setString(i++, datetime.getDate());
            //execute SQL statement
            pstmt.executeUpdate();
            conn.close();
            result2 = true;

        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return result2;
    }

    public boolean editUserLock(User newUser) {
        boolean result = false;

        try {
            //get connection
            DBConnection db = new DBConnection();
            Connection conn = db.getConnection();

            int i = 1;

            PreparedStatement pstmt = conn.prepareStatement("UPDATE user SET Status = 0 WHERE AccountID=?");

            //   pstmt.setInt(i++, newUser.getStatus());
            pstmt.setString(i++, newUser.getAccountID());
            pstmt.executeUpdate();

            result = true;
            conn.close();

        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return result;
    }

    public boolean editUserUnlock(User newUser2) {
        boolean result = false;

        try {
            //get connection
            DBConnection db = new DBConnection();
            Connection conn = db.getConnection();

            int i = 1;

            PreparedStatement pstmt = conn.prepareStatement("UPDATE user SET Status = 1 WHERE AccountID=?");

            //   pstmt.setInt(i++, newUser.getStatus());
            pstmt.setString(i++, newUser2.getAccountID());
            pstmt.executeUpdate();

            result = true;
            conn.close();

        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return result;
    }

    public boolean addLocklog(DateTime datetime) {
        boolean result2 = false;

        try {
            //get connection
            Model.DBConnection db = new DBConnection();
            Connection conn = db.getConnection();

            PreparedStatement pstmt = conn.prepareStatement("INSERT "
                    + "INTO locklog(AccountID,Time,Date)"
                    + " values(?,?,?)");

            //append values for the parameters
            int i = 1;

            pstmt.setString(i++, datetime.getAccountID());
            pstmt.setString(i++, datetime.getTime());
            pstmt.setString(i++, datetime.getDate());

            //execute SQL statement
            pstmt.executeUpdate();
            conn.close();
            result2 = true;

        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return result2;
    }

    public boolean addUnlocklog(DateTime datetime) {
        boolean result2 = false;

        try {
            //get connection
            Model.DBConnection db = new DBConnection();
            Connection conn = db.getConnection();

            PreparedStatement pstmt = conn.prepareStatement("INSERT "
                    + "INTO unlocklog(AccountID,Time,Date)"
                    + " values(?,?,?)");

            //append values for the parameters
            int i = 1;

            pstmt.setString(i++, datetime.getAccountID());
            pstmt.setString(i++, datetime.getTime());
            pstmt.setString(i++, datetime.getDate());

            //execute SQL statement
            pstmt.executeUpdate();
            conn.close();
            result2 = true;

        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return result2;
    }

    public boolean checkManagerLogin24(User newUser2, long time){
        boolean result = false;
        
        try {
            //get connection
            Model.DBConnection db = new DBConnection();
            Connection conn = db.getConnection();

            PreparedStatement pstmt = conn.prepareStatement("SELECT Time, Date FROM addmanager WHERE AccountID = ?");
            int i = 1;
            pstmt.setString(i++, newUser2.getAccountID());
            ResultSet rs = pstmt.executeQuery();
            rs.next();
            long timecreate = rs.getTime(1).getTime();
            long datecreate = rs.getDate(2).getTime();
            long createtime = timecreate + datecreate;
            long loginsubcreate = time - createtime; 
            if(loginsubcreate <= 86400000){
            result = true;
            }
            conn.close();
            
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return result;
    }
}
